﻿Imports Entities
Public Class ItemDAL
    Public Shared Function List() As List(Of ITEMES)
        Try
            Using bd As New gas_gasdesuvecinoEntities
                Dim query = From rec In bd.ITEMES
                              Where rec.ACTIVO
                                            Select rec

                Return query.ToList
            End Using
        Catch ex As Exception
            Debug.Print("ItemDAL.List() " + ex.Message)
            Return Nothing
        End Try
    End Function
    Public Shared Function ListByProveedorId(ByVal zId As Long) As List(Of ITEMES)
        Try
            Using bd As New gas_gasdesuvecinoEntities
                Dim query = (From rec In bd.ITEMES
                            Where rec.PROVEEDOR = zId
                            Select rec).ToList
                Return query
            End Using
        Catch ex As Exception
            Debug.Print("ItemDAL.List() " + ex.Message)
            Return Nothing
        End Try
    End Function
    Public Shared Function Create(ByVal zObject As ITEMES) As Boolean
        Try
            Using bd As New gas_gasdesuvecinoEntities
                zObject.ACTIVO = True
                bd.ITEMES.Add(zObject)
                bd.SaveChanges()
                Return True
            End Using
        Catch ex As Exception
            Debug.Print("ItemDAL.Create() " + ex.Message)
            Return False
        End Try
    End Function
    Public Shared Function Update(ByVal zObject As ITEMES) As ITEMES
        Try
            Using bd As New gas_gasdesuvecinoEntities
                Dim query = (From rec In bd.ITEMES
                              Where rec.ITEM = zObject.ITEM
                              Select rec).Single

                query.PROVEEDOR = zObject.PROVEEDOR
                query.GARANTIA = zObject.GARANTIA
                query.DESCRIPCION = zObject.DESCRIPCION
                query.CANTIDADKILOS = zObject.CANTIDADKILOS
                query.STOCK = zObject.STOCK
                query.PRECIOVENTA = zObject.PRECIOVENTA
                bd.SaveChanges()
                Return query
            End Using
        Catch ex As Exception
            Debug.Print("ItemDAL.Update() " + ex.Message)
            Return Nothing
        End Try
    End Function
    Public Shared Function Delete(ByVal zId As Long) As Boolean
        Try
            Using bd As New gas_gasdesuvecinoEntities
                Dim del = (From rec In bd.ITEMES
                          Where rec.ITEM = zId
                          Select rec).Single

                bd.ITEMES.Remove(del)
                bd.SaveChanges()
                Return True
            End Using
        Catch ex As Exception
            Debug.Print("ItemDAL.Delete() " + ex.Message)
            Return False
        End Try
    End Function
    Public Shared Function Exist(ByVal zId As Long) As Boolean
        Try
            Using bd As New gas_gasdesuvecinoEntities
                Dim query = (From rec In bd.ITEMES
                            Where rec.ITEM = zId
                            Select rec).Count
                If query > 0 Then
                    Return True
                Else
                    Return False
                End If
            End Using
        Catch ex As Exception
            Debug.Print("ItemDAL.Exist() " + ex.Message)
            Return False
        End Try
    End Function

    Public Shared Function SearchById(ByVal zId As Long) As ITEMES
        Try
            Using bd As New gas_gasdesuvecinoEntities
                Return bd.ITEMES.Find(zId)
            End Using
        Catch ex As Exception
            Debug.Print("ItemDAL.SearchById() " + ex.Message)
            Return Nothing
        End Try
    End Function

    Public Shared Function ListByGarantiaId(ByVal zId As Long) As List(Of ITEMES)
        Try
            Using bd As New gas_gasdesuvecinoEntities
                Dim query = (From rec In bd.ITEMES
                            Where rec.GARANTIA = zId
                             Order By rec.DESCRIPCION Ascending
                            Select rec).ToList
                Return query
            End Using
        Catch ex As Exception
            Debug.Print("ItemDAL.List() " + ex.Message)
            Return Nothing
        End Try
    End Function

End Class
